package utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


    /**
    * @ClassName: UtilController
    * @Description: MVC 层中contrller 常用工具类
    * @author 宁少林
    * @date 2016年8月21日
    *
    */
public class UtilController {
	
	
	    /**
	    * @Title: getSession
	    * @Description: springMvc 环境下获得session
	    * @param @return    
	    * @return HttpSession    
	    * @throws
	    */
	public static HttpSession getSession() { 
		HttpSession session = null; 
		try { 
		    session = getRequest().getSession(); 
		} catch (Exception e) {
			System.out.println(e);
		} 
		return session; 
	} 

	
	    /**
	    * @Title: getRequest
	    * @Description: springMvc 环境下获得HttpSerletRequest
	    * @param @return    
	    * @return HttpServletRequest    
	    * @throws
	    */
	public static HttpServletRequest getRequest() { 
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder 
		.getRequestAttributes(); 
		return attrs.getRequest(); 
	}
	
	//下载
	
	    /**
	    * @Title: downloadFile
	    * @Description: 下载文件
	    * @param @param fileAbsPath 本地文件绝对路径
	    * @param @param savedName 下载文件名
	    * @param @param request HttpServletRequest
	    * @param @param response HttpServletResponse
	    * @param @throws IOException    
	    * @return void    
	    * @throws
	    */
	public static void downloadFile(String fileAbsPath,String savedName,
			HttpServletRequest request, HttpServletResponse response) throws IOException{
        BufferedInputStream bis = null;  
        BufferedOutputStream bos = null;  
  
        File downloadFile=new File(fileAbsPath);
        long fileLength = downloadFile.length();  
       
        response.setHeader("Content-disposition", "attachment; filename="  
                + new String(savedName.getBytes("utf-8"), "ISO8859-1"));  
        response.setHeader("Content-Length", String.valueOf(fileLength));  
  
        bis = new BufferedInputStream(new FileInputStream(downloadFile));  
        bos = new BufferedOutputStream(response.getOutputStream());  
        byte[] buff = new byte[2048];  
        int bytesRead;  
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
            bos.write(buff, 0, bytesRead);  
        }  
        bis.close();  
        bos.close(); 
	}
	
	
	 /**
	    * @Title: getImage
	    * @Description: 返回图片文件
	    * @param @param fileAbsPath 本地文件绝对路径
	    * @param @param request HttpServletRequest
	    * @param @param response HttpServletResponse
	    * @param @throws IOException    
	    * @return void    
	    * @throws
	    */
	public static void getImage(String fileAbsPath,HttpServletRequest request,HttpServletResponse response) {
	    FileInputStream fis = null;
	    response.setContentType("image/gif");
	    try {
	        OutputStream out = response.getOutputStream();
	        File file = new File(fileAbsPath);
	        fis = new FileInputStream(file);
	        byte[] b = new byte[fis.available()];
	        fis.read(b);
	        out.write(b);
	        out.flush();
	    } catch (Exception e) {
	         e.printStackTrace();
	    } finally {
	        if (fis != null) {
	            try {
	               fis.close();
	            } catch (IOException e) {
		        e.printStackTrace();
		    }   
	          }
	    }
	}
}
